package com.github.vortexdev.yachtclub.controllers;

import com.github.vortexdev.yachtclub.controllers.exceptions.UploadPhotoException;
import com.github.vortexdev.yachtclub.models.Owner;
import com.github.vortexdev.yachtclub.models.Regatta;
import com.github.vortexdev.yachtclub.models.Yacht;
import com.github.vortexdev.yachtclub.services.OwnerService;
import com.github.vortexdev.yachtclub.services.RegattaService;
import com.github.vortexdev.yachtclub.services.YachtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Sergey Parakhin (sergey.parahin@gmail.com)
 */
@Controller
@RequestMapping(value = "/new")
public class YachtFormController {

    @Autowired
    YachtService yachtService;

    @Autowired
    OwnerService ownerService;

    @Autowired
    RegattaService regattaService;

    @RequestMapping(method = RequestMethod.GET)
    public String yachtWizard(Model model) {
        model.addAttribute("owners", ownerService.getOwners());
        model.addAttribute("regattas", regattaService.getRegattas());
        return "add_yacht";
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Yacht onSubmit(@RequestParam(value = "photo", required = false) MultipartFile multipartFile,
                          HttpServletRequest request) throws UploadPhotoException {
        Yacht yacht = new Yacht();
        yacht.setName(request.getParameter("yacht_name"));
        yacht.setHull(request.getParameter("hull"));
        yacht.setDrive(request.getParameter("drive"));
        yacht.setPurpose(request.getParameter("purpose"));
        yacht.setDescription(request.getParameter("description"));
        if (request.getParameter("owner") != null)
            yacht.setOwner(
                    new Owner(Integer.valueOf(request.getParameter("owner")))
            );

        // parse string with regattas ids separated by ';'  :
        String[] regattaIDs = request.getParameter("regatta_ids").split(";");
        String regatta;
        // add regattas to yacht:
        for (String id : regattaIDs) {
            if ((regatta = request.getParameter("regatta-" + id)) != null)
                yacht.addRegatta(
                        new Regatta(Integer.valueOf(regatta))
                );
        }

        // set photo
        try {
            yacht.setPhoto(multipartFile.getBytes());
        } catch (IOException e) {
            throw new UploadPhotoException(e.getMessage());
        }

        yacht.setContentType(multipartFile.getContentType());

        yachtService.saveYacht(yacht);

        // defense from Stack overflow (regatta also contains yachts)
        // when json will created:
        yacht.getRegattas().clear();

        return yacht;
    }

}
